/*
 * SpeedoDyno - Speedometer and dynamometer application for Android 
 * Copyright 2013 Guilhem VALENTIN
 * 
 * This file is part of SpeedoDyno
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SpeedoDyno. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gvalentin.speedodyno;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;

/**
 * Activity to manage preferences
 * @author guilhem
 */
public class SpeedoDynoPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

	/**
	 * Tie the activity with the XML layout file
	 * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {    
	    super.onCreate(savedInstanceState);       
	    addPreferencesFromResource(R.xml.preferences);       
	}

    /**
     * On start: automatically set summaries and start listening for updates
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        super.onResume();

        // Initialize fields summary
        SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
        for (String key : sharedPreferences.getAll().keySet()) {
        	initSummary(sharedPreferences, key);
        }
        
        // Set up a listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }
    
    /**
     * Initialize the summary of a field
     */
    private void initSummary(SharedPreferences sharedPreferences, String key) {
    	if ("pref_logging".equals(key)) {
    		setSummary(sharedPreferences, "pref_logging", R.string.preferences_pref_logging_summary_true, R.string.preferences_pref_logging_summary_false);
    	} else {
    		// For all other fields, use value as summary
            setSummary(sharedPreferences, key);
    	}
    }

    /**
     * On stop: stop listening for updates
     * @see android.app.Activity#onPause()
     */
    @Override
    protected void onPause() {
        super.onPause();

        // Unregister the listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);    
    }
	
	/**
	 * Action performed when a preference is changed
	 */
    @Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    	initSummary(sharedPreferences, key);
	}
	
	
	//
	// Methods to set fields summary
	//
	
	/**
	 * Set summary of a field by using its current value
	 */
	private void setSummary(SharedPreferences sharedPreferences, String key) {
		// Get value
		String value = sharedPreferences.getString(key, "");
    	
    	// Set summary in preference field
		Preference pref = getPreferenceScreen().findPreference(key);
		if (pref != null) {
			pref.setSummary(value);
		}
	}
	
	/**
	 * Set summary of a CheckBox field depending on its current value
	 */
	private void setSummary(SharedPreferences sharedPreferences, String key, int resourceTrue, int resourceFalse) {
		// Get value
		boolean value = sharedPreferences.getBoolean(key, false);
		
		// Determine summary depending on value
    	int summaryId = (value ? resourceTrue : resourceFalse);
    	String summary = getResources().getString(summaryId);
    	
    	// Set summary in preference field
		CheckBoxPreference pref = (CheckBoxPreference) getPreferenceScreen().findPreference(key);
		pref.setSummary(summary);
	}
}
